package com.yfqy.admin.resource;

import com.github.pagehelper.PageInfo;
import com.yfqy.admin.domain.dto.QueryById;
import com.yfqy.admin.domain.dto.category.AddCategory;
import com.yfqy.admin.domain.dto.category.CategoryDTO;
import com.yfqy.admin.domain.dto.category.CategoryQuery;
import com.yfqy.admin.domain.dto.category.ModifyCategory;
import com.yfqy.admin.service.CategoryService;
import com.yfqy.admin.domain.vo.common.ResultVO;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商品分类管理
 */
@RestController
@RequestMapping("/api/internal/category")
public class CategoryController {

    private static final Logger LOGGER = LoggerFactory.getLogger(CategoryController.class);

    @Resource
    private CategoryService categoryService;

    /**
     * 商品分类详情
     *
     * @param id
     * @return
     */
    @GetMapping({"/selectById"})
    public ResultVO<CategoryDTO> selectById(@RequestParam("id") Long id) {
        CategoryDTO categoryDTO = categoryService.selectByPrimaryKey(id);
        LOGGER.info("======分类详情======:{}", categoryDTO);
        return ResultVO.data(categoryDTO);
    }

    /**
     * 商品分类分页列表
     *
     * @param categoryQuery
     * @return
     */
    @PostMapping("/pageListByName")
    public ResultVO<PageInfo<CategoryDTO>> pageListByName(@RequestBody CategoryQuery categoryQuery) {
        return ResultVO.data(categoryService.pageListByName(categoryQuery));
    }

    /**
     * 添加商品分类
     *
     * @param addCategory
     * @return
     */
    @PostMapping("/add")
    public ResultVO<String> addCategory(@RequestBody @Validated AddCategory addCategory) {
        try {
            int insert = categoryService.addCategory(addCategory);
            return insert > 0 ? ResultVO.success("添加成功") : ResultVO.fail("添加失败");
        } catch (IllegalArgumentException e) {
            return ResultVO.fail(e.getMessage());
        }
    }


    /**
     * 修改商品分类
     *
     * @param modifyCategory
     * @return
     */
    @PostMapping("/modify")
    public ResultVO<String> modifyCategory(@RequestBody @Validated ModifyCategory modifyCategory) {
        int update = categoryService.modifyCategory(modifyCategory);
        return update > 0 ? ResultVO.success("修改成功") : ResultVO.fail("修改失败");
    }


    /**
     * 删除商品分类
     *
     * @param queryById
     * @return
     */
    @PostMapping("/delete")
    public ResultVO<String> deleteCategory(@RequestBody @Validated QueryById queryById) {
        int deleted = categoryService.deleteCategory(queryById.getId());
        return deleted > 0 ? ResultVO.success("删除成功") : ResultVO.fail("删除失败");
    }


}
